﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Aspose.Excel;
using log4net.Appender;
using Telerik.Web.UI;

public partial class EmailDeliveryDetailsReport : BaseReportPage
{
    public override string Icon
    {
        get
        {
            return "images/report_icon.png";
        }
    }

	private bool _isExporting;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
        	DateTime date;

			if (DateTime.TryParse(Request.Params["d"], out date))
				InitlblDate(date);
			else
				InitlblDate(DateTime.Today);

			if (Request.Params["s"] != null)
			{
				rbtnStatus.SelectedValue = Request.Params["s"] == "all" ? "-1" : Request.Params["s"];
			}
        }
    }

    void InitlblDate(DateTime date)
    {
    	lblDate.Text = date.ToShortDateString();
    }

    protected void lbExport_Click(object sender, ImageClickEventArgs e)
    {
		/*_isExporting = true;
		ExportToExcel("EmailDeliveryDetailsReport", rgEmailDeliveryDetailsReport);*/

		Fill();

		DataTable dt = rgEmailDeliveryDetailsReport.DataSource as DataTable;

		if (dt != null)
		{
			int startRow = 1;
			string[] listFields = new string[]
			                      	{
			                      		"STUDENT_ID", "LASTFIRST", "BANV_EMAIL", "PERSONAL_EMAIL", "PARENT_EMAIL"
			                      	};
			Excel ex = Utils.GridToExcel(dt, listFields, "EmailDeliveryDetailsReport", false, ref startRow);
			Worksheet ws = ex.Worksheets[0];
			for (int i = 1; i < startRow - 1; ++i)
			{
				DataRow row = dt.Rows[i - 1];
				for (int j = 0; j < dt.Columns.Count; ++j)
				{
					int dtColumn = j;
					if (row.Table.Columns[dtColumn].ColumnName == "BANV_EMAIL")
					{
						decimal iStatusBanv = row["BANV_STATUS"] is DBNull ? -1 : (decimal)row["BANV_STATUS"];
						if (iStatusBanv == Convert.ToDecimal(EmailDeliveryStatus.Failed))
							ws.Cells[i, j - 2].Style.ForegroundColor = Color.Red;
						else if (iStatusBanv == Convert.ToDecimal(EmailDeliveryStatus.Delivered))
							ws.Cells[i, j - 2].Style.ForegroundColor = Color.Green;

						ws.Cells[i, j - 2].Style.Font.Color = Color.White;
					}

					if (row.Table.Columns[dtColumn].ColumnName == "PERSONAL_EMAIL")
					{
						decimal iStatusBanv = row["PERSONAL_STATUS"] is DBNull ? -1 : (decimal)row["PERSONAL_STATUS"];
						if (iStatusBanv == Convert.ToDecimal(EmailDeliveryStatus.Failed))
							ws.Cells[i, j - 3].Style.ForegroundColor = Color.Red;
						else if (iStatusBanv == Convert.ToDecimal(EmailDeliveryStatus.Delivered))
							ws.Cells[i, j - 3].Style.ForegroundColor = Color.Green;

						ws.Cells[i, j - 3].Style.Font.Color = Color.White;
					}

					if (row.Table.Columns[dtColumn].ColumnName == "PARENT_EMAIL")
					{
						decimal iStatusBanv = row["PARENT_STATUS"] is DBNull ? -1 : (decimal)row["PARENT_STATUS"];
						if (iStatusBanv == Convert.ToDecimal(EmailDeliveryStatus.Failed))
							ws.Cells[i, j - 4].Style.ForegroundColor = Color.Red;
						else if (iStatusBanv == Convert.ToDecimal(EmailDeliveryStatus.Delivered))
							ws.Cells[i, j - 4].Style.ForegroundColor = Color.Green;

						ws.Cells[i, j - 4].Style.Font.Color = Color.White;
					}

					if (j < listFields.Length)
					{
						ws.Cells[i, j].Style.Borders[BorderType.LeftBorder].Color = Color.Black;
						ws.Cells[i, j].Style.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;

						ws.Cells[i, j].Style.Borders[BorderType.RightBorder].Color = Color.Black;
						ws.Cells[i, j].Style.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;

						ws.Cells[i, j].Style.Borders[BorderType.BottomBorder].Color = Color.Black;
						ws.Cells[i, j].Style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;

						ws.Cells[i, j].Style.HorizontalAlignment = TextAlignmentType.Center;
					}
				}
			}



			ex.Save("EmailDeliveryDetailsReport Download.xls", FileFormatType.Excel2003, SaveType.OpenInExcel, Page.Response);
			Page.Response.End();
		}
    }

    protected void rgEmailDeliveryDetailsReport_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
    {
		Fill();
    }

    protected void rbtnStatus_SelectedIndexChanged(object sender, EventArgs e)
    {
		Fill();
		rgEmailDeliveryDetailsReport.Rebind();
    }
	protected void ibtnNextDate_Click(object sender, ImageClickEventArgs e)
    {
		if (!string.IsNullOrEmpty(hdnNextDate.Value))
		{
			DateTime date = Convert.ToDateTime(hdnNextDate.Value);
			Response.Redirect(Request.RawUrl.Replace(Request.Params["d"], date.ToString("yyyy-MM-dd")));
		}
    }
	protected void ibtnPrevDate_Click(object sender, ImageClickEventArgs e)
    {
		if (!string.IsNullOrEmpty(hdnPrevDate.Value))
		{
			DateTime date = Convert.ToDateTime(hdnPrevDate.Value);
			Response.Redirect(Request.RawUrl.Replace(Request.Params["d"], date.ToString("yyyy-MM-dd")));
		}
    }

	protected override void Fill()
	{
		string message;

		decimal? status = rbtnStatus.SelectedValue == "-1" ? null : (decimal?) Convert.ToDecimal(rbtnStatus.SelectedValue);

		var dt = new DataLayer().GetEmailDeliveryDetails(PAConfiguration.ConnectionString, Convert.ToDateTime(lblDate.Text),
																						  status, out message);

		ibtnPrevDate.Visible = false;
		ibtnNextDate.Visible = false;

		if (dt != null && dt.Rows.Count > 0)
		{
			ibtnPrevDate.Visible = dt.Rows[0]["PREV_DATE"] != DBNull.Value;
			ibtnNextDate.Visible = dt.Rows[0]["NEXT_DATE"] != DBNull.Value;

			hdnPrevDate.Value = dt.Rows[0]["PREV_DATE"].ToString();
			hdnNextDate.Value = dt.Rows[0]["NEXT_DATE"].ToString();
		}

		rgEmailDeliveryDetailsReport.DataSource = dt;
	}

	protected void reportGrid_ItemCreated(object sender, GridItemEventArgs e)
	{
		RadGrid grid = sender as RadGrid;

		if (e.Item is GridDataItem)
		{
			DataRowView drv = e.Item.DataItem as DataRowView;
			if (drv != null)
			{
				if (_isExporting)
				{
					foreach (TableCell c in e.Item.Cells)
					{
						c.HorizontalAlign = HorizontalAlign.Center;
					}

					e.Item.Cells[3].Text = drv["LASTFIRST"].ToString();
					e.Item.Cells[4].ForeColor = Color.FromName("White");
					e.Item.Cells[5].ForeColor = Color.FromName("White");
					e.Item.Cells[6].ForeColor = Color.FromName("White");
				}

				decimal iStatusBanv = drv["BANV_STATUS"] is DBNull ? -1 : (decimal)drv["BANV_STATUS"];

				if (iStatusBanv == Convert.ToDecimal(EmailDeliveryStatus.Failed))
				{
					e.Item.Cells[4].BackColor = Color.FromName("Red");
				}
				else if (iStatusBanv == Convert.ToDecimal(EmailDeliveryStatus.Delivered))
				{
					e.Item.Cells[4].BackColor = Color.FromName("Green");
				}

				decimal iStatusPerson = drv["PERSONAL_STATUS"] is DBNull ? -1 : (decimal)drv["PERSONAL_STATUS"];

				if (iStatusPerson == Convert.ToDecimal(EmailDeliveryStatus.Failed))
				{
					e.Item.Cells[5].BackColor = Color.FromName("Red");
				}
				else if (iStatusPerson == Convert.ToDecimal(EmailDeliveryStatus.Delivered))
				{
					e.Item.Cells[5].BackColor = Color.FromName("Green");
				}

				decimal iStatusParent = drv["PARENT_STATUS"] is DBNull ? -1 : (decimal)drv["PARENT_STATUS"];

				if (iStatusParent == Convert.ToDecimal(EmailDeliveryStatus.Failed))
				{
					e.Item.Cells[6].BackColor = Color.FromName("Red");
				}
				else if (iStatusParent == Convert.ToDecimal(EmailDeliveryStatus.Delivered))
				{
					e.Item.Cells[6].BackColor = Color.FromName("Green");
				}
			}
		}
	}

}